<template>
  <div class="container">
    <el-card class="page-tools" v-loading="loading">
      <el-row type="flex" justify="space-between" align="middle">
        <el-col>
          <div class="before">
            <span class="keywords">关键字</span>
            <el-input
              style="width: 200px"
              size="small"
              v-model="page.keyword"
              placeholder="根据编号搜索"
              @keydown.enter.native="searchBtn"
            ></el-input>
          </div>
        </el-col>
        <el-col>
          <el-row type="flex" justify="end">
            <el-button size="small" @click="clearBtn">清除</el-button>
            <el-button size="small" type="primary" @click="searchBtn"
              >搜索</el-button
            >
          </el-row>
        </el-col>
      </el-row>
      <el-row class="alert">
        <el-alert
          :title="`数据一共 ${this.page.total} 条`"
          type="info"
          class="alert"
          show-icon
        ></el-alert>
      </el-row>

      <el-table
        border
        :data="list"
        class="table"
        :header-cell-style="{ background: '#fafafa', color: '#6060266' }"
      >
        <el-table-column label="编号" prop="id" width="200" align="center" />
        <el-table-column
          label="题型"
          prop="questionType"
          width="100"
          align="center"
          :formatter="formatQuestionType"
        />

        <el-table-column
          label="题目编号"
          prop="questionIDs"
          align="center"
          width="250"
        >
          <template v-slot="{ row }">
            <div v-for="(item, index) in row.questionIDs" :key="index">
              <a
                href="#"
                style="color: rgb(0, 153, 255)"
                @click="openDialog(item.id)"
              >
                {{ item.number }}
              </a>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addTime" align="center" />
        <el-table-column
          label="答题时间(s)"
          prop="totalSeconds"
          align="center"
        />
        <el-table-column label="正确率(%)" prop="accuracyRate" align="center" />
        <el-table-column label="录入人" prop="userName" align="center" />
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="{ row }">
            <el-button type="danger" size="small" @click="delBtn(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center">
        <page-tool
          style="margin-top: 20px"
          :paginationPage="page.page"
          :paginationPagesize="page.pagesize"
          :total="page.total"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        >
        </page-tool>
      </el-row>
    </el-card>
    <question-preview
      :showDialog.sync="showDialog"
      :questionId="questionId"
      ref="Dialog"
    ></question-preview>
  </div>
</template>

<script>
import PageTool from "../../module-manage/components/page-tool.vue";
import QuestionPreview from "../components/questions-preview.vue";
import { randoms, removeRandoms } from "@/api/hmmm/questions";
import { questionType } from "@/api/hmmm/constants";
export default {
  data() {
    return {
      list: [], //接受数据的数组
      loading: false,
      page: {
        page: 1, // 当前页码
        pagesize: 10,
        total: 0, // 总数
        keyword: "",
      },
      showDialog: false,
      questionId: 1,
    };
  },
  components: {
    PageTool,
    QuestionPreview,
  },
  created() {
    this.randoms();
  },
  methods: {
    // 渲染数据
    async randoms() {
      this.loading = true;
      const { data } = await randoms(this.page);
      this.list = data.items;
      this.page.total = data.counts;
      this.loading = false;
      console.log(data);
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.page.pagesize = val;
      if (this.page.page === 1) {
        this.randoms(this.page);
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.page.page = val;
      this.randoms();
    },
    // 格式化题型
    formatQuestionType(row, column, cellValue, index) {
      // 找3对应得值
      const obj = questionType.find((item) => item.value * 1 === cellValue * 1);
      return obj ? obj.label : "未知";
    },
    clearBtn() {
      this.page.keyword = "";
    },
    // 搜索信息
    searchBtn() {
      this.page.page = 1;
      this.randoms(this.page);
    },
    // 删除
    async delBtn(id) {
      try {
           await this.$confirm("此操作将永久删除该文章, 是否继续？", "提示", {
          type: "warning",
        });
        await removeRandoms({ id: id });
        this.randoms();
        this.$message.success("删除员工成功");
      } catch (error) {
        console.log(error);
      }
    },
    openDialog(id) {
      this.questionId = id;
      // console.log(this.questionId);
      this.$refs.Dialog.getQuestionDetail(id);
       this.showDialog = true;
    },
  },
};
</script>

<style scoped lang='scss'>
.page-tools {
  margin: 10px 0;
  .before {
    line-height: 34px;
    display: inline-block;
    padding: 0px 10px;
    border-radius: 3px;
    .keywords {
      font-size: 14px;
      font-weight: 700;
      margin: 0 15px;
    }
  }
  .alert {
    margin-top: 10px;
  }
  .table {
    margin-top: 20px;
  }
}
</style>
